<xsl:stylesheet version='2.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform' xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:csharp="http://csharp.org" xmlns:cot='urn:cot:mitre:org' xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xsl:import href="get-address.xsl"/>
<xsl:import href="format-date.xsl"/>
	<msxsl:script language="CSharp" implements-prefix="csharp"><![CDATA[

	public bool matchWildcard(string input, string pattern, string wildcard)
	{
  // added the ToUppers to ignore case in all operations. May need to be removed if the case matters and it used to demark different unit types.
		pattern = pattern.ToUpper();
    input = input.ToUpper();
    
		if(input.Length != pattern.Length){
			return false;
		}
		
		if(! (pattern.Contains(wildcard)) ){
			return false;
		}
		
		
		int wildcardIndex = pattern.IndexOf(wildcard);

		string a1 = input.Substring(0, wildcardIndex-1);
		string a2 = pattern.Substring(0, wildcardIndex-1);
		
		string b1 = input.Substring(wildcardIndex+1, input.Length-(wildcardIndex+1) );
		string b2 = pattern.Substring(wildcardIndex+1, input.Length-(wildcardIndex+1) );

		bool firstMatches = a1.Equals(a2);
		bool secondMatches = b1.Equals(b2);
		

		return ( secondMatches && firstMatches );
		
	}
	]]></msxsl:script>
	<xsl:output method="html" indent="yes" exclude-result-prefixes="msxsl csharp"/>
	<xsl:template match="*">
		<xsl:variable name="allCoTTypes" select="document('CoTtypes_CIVOnly.xml')/types"/>
		<xsl:variable name="type">
			<xsl:value-of select="@type"/>
		</xsl:variable>
		<xsl:variable name="typeDescription">
			<xsl:choose>
				<xsl:when test="count($allCoTTypes/cot[@cot = $type]) > 0">
					<xsl:value-of select="$allCoTTypes/cot[@cot = $type]/@desc"/>
				</xsl:when>
				<xsl:otherwise>
					<xsl:for-each select="$allCoTTypes/cot">
						<xsl:choose>
							<xsl:when test="csharp:matchWildcard($type, @cot, '.')">
								<xsl:value-of select="@desc"/>
							</xsl:when>
						</xsl:choose>
					</xsl:for-each>
				</xsl:otherwise>
			</xsl:choose>
		</xsl:variable>
		<xsl:variable name="how">
			<xsl:value-of select="@how"/>
		</xsl:variable>
		<xsl:variable name="howWhat">
			<xsl:choose>
				<xsl:when test="count($allCoTTypes/how[@value = $how]) > 0">
					<xsl:value-of select="$allCoTTypes/how[@value = $how]/@what"/>
				</xsl:when>
				<xsl:otherwise>
            Unknown?
          </xsl:otherwise>
			</xsl:choose>
		</xsl:variable>
		<xsl:variable name="serverBaseURL">http://icnet-test.mitre.org</xsl:variable>
		<xsl:variable name="allStyles" select="document('../Resources/ICNET_KMLStyles.xml')/ICNET_Styles"/>
		<xsl:variable name="styleMatch" select="$allStyles/ICNET_Style/Match_Info[starts-with($type, Pattern)]"/>
		<xsl:variable name="styleDefault" select="$allStyles/ICNET_Style/Match_Info['*.*' = Pattern]"/>
		<xsl:variable name="iconURL">
			<xsl:choose>
				<xsl:when test="not($styleMatch)">
				
					<xsl:choose>
						<xsl:when test="not($styleDefault/parent::node()/Style/IconStyle/Icon/href)">
							<xsl:variable name="normalIconID">
								<xsl:value-of select="substring($styleDefault/parent::node()/StyleMap/Pair[key = 'normal']/styleUrl, 2)"/>
							</xsl:variable>
							<xsl:value-of select="$allStyles/ICNET_Style/Style[@id = $normalIconID]/IconStyle/Icon/href"/>
						</xsl:when>
						<xsl:otherwise>
							<xsl:value-of select="$styleDefault/parent::node()/Style/IconStyle/Icon/href"/>
						</xsl:otherwise>
					</xsl:choose>
				</xsl:when>
				
				<xsl:otherwise>
					<xsl:choose>
						<xsl:when test="not($styleMatch/parent::node()/Style/IconStyle/Icon/href)">
							<xsl:variable name="normalIconID">
								<xsl:value-of select="substring($styleMatch/parent::node()/StyleMap/Pair[key = 'normal']/styleUrl, 2)"/>
							</xsl:variable>
							<xsl:value-of select="$allStyles/ICNET_Style/Style[@id = $normalIconID]/IconStyle/Icon/href"/>
						</xsl:when>
						<xsl:otherwise>
							<xsl:value-of select="$styleMatch/parent::node()/Style/IconStyle/Icon/href"/>
						</xsl:otherwise>
					</xsl:choose>
				</xsl:otherwise>
			</xsl:choose>
		</xsl:variable>
		<html>
			<head>
			</head>
			<body>
				<div style="font-family: sans-serif; border-top: 1px solid DimGray; border-left: 1px solid DimGray; border-right: 1px solid DimGray; min-width: 310px;">
					<div>
												<xsl:choose>
							<xsl:when test="@type = 'a-f-g-9-1-1'">
								<div style="color: white; background-color: #dd0000; text-align: center; border-bottom: 1px solid DimGray; padding: 0.25em;">
									<img border="0" alt="icon" style="float: left; max-height: 37px;">
										<xsl:attribute name="src"><xsl:value-of select="$serverBaseURL"/>/Resources/911.png</xsl:attribute>
									</img>
									<h1 style="display: inline; padding: 0 0.5em 0 0; margin: 0; text-align: center;">
										  <xsl:value-of select="@uid"/> - MAYDAY
									</h1>
								</div>
							</xsl:when>
							<xsl:otherwise>
								<div style="color: white; background-color: black; text-align: center; border-bottom: 1px solid DimGray; padding: 0.25em;">
									<img border="0" alt="icon" style="float: left; max-height: 37px;">
										<xsl:attribute name="src"><xsl:value-of select="$serverBaseURL"/><xsl:value-of select="$iconURL"/></xsl:attribute>
									</img>
									<h1 style="padding: 0; margin: 0; text-align: center;">
										<xsl:value-of select="@uid"/><xsl:if test="$typeDescription != ''"> - <xsl:value-of select="$typeDescription"/></xsl:if>
									</h1>
								</div>
							</xsl:otherwise>
						</xsl:choose>
						<div style="text-align: center; border-bottom: 1px solid DimGray; padding: 0.25em;">Near <span style="font-weight: bold;">
								<xsl:variable name="lat">
									<xsl:choose>
										<xsl:when test="point/@lat"><xsl:value-of select="point/@lat"/></xsl:when>
										<xsl:otherwise><xsl:value-of select="cot:point/@lat"/></xsl:otherwise>
									</xsl:choose>
								</xsl:variable>
								<xsl:variable name="lon">
									<xsl:choose>
										<xsl:when test="point/@lon"><xsl:value-of select="point/@lon"/></xsl:when>
										<xsl:otherwise><xsl:value-of select="cot:point/@lon"/></xsl:otherwise>
									</xsl:choose>
								</xsl:variable>
						
								<xsl:variable name="googleAddress"><xsl:value-of select="csharp:GetAddressFromLatLon(
										$lat,
										$lon)"/>
								</xsl:variable>
									<xsl:if test="$googleAddress != ''">
										<xsl:value-of select="$googleAddress"/>
									</xsl:if>
								<div style="font-weight: normal; font-size: 0.8em;"><xsl:value-of select="$lat"/>, <xsl:value-of select="$lon"/></div>
							</span>
						</div>
					</div>
					<div>
						<div style="text-align: center; border-bottom: 1px solid DimGray; padding: 0.25em;">
							<p style="padding: 0; margin: 0; text-align: center;">
								<strong>Sent on </strong>
                <xsl:value-of select="csharp:FormatDateTime(@time)"/>
							</p>
						</div>
						<div style="border-bottom: 1px solid DimGray; padding: 0.25em;">
							<xsl:choose>
								<xsl:when test="@type = 'a-f-g-9-1-1'">
									<h3 style="padding: 0; margin: 0; text-align: center;">MAYDAY Message</h3>
								</xsl:when>
								<xsl:otherwise>
									<h3 style="padding: 0; margin: 0; text-align: center;">Point Message</h3>
								</xsl:otherwise>
							</xsl:choose>
							<div style="padding: 0 0 0 0.5em; min-width:100%; max-width:100%; vertical-align:top;">
								<table style="margin-left:auto; margin-right:auto;">
									<xsl:if test="cot:detail/cot:remarks != ''">
										<tr>
											<th style="text-align: left; width: 125px; vertical-align:top;">Remarks:</th>
											<td>
												<xsl:value-of select="cot:detail/cot:remarks"/>
											</td>
										</tr>
									</xsl:if>
									<xsl:for-each select="cot:detail/cot:__icnet/cot:beacon">
										<tr>
											<th style="text-align: left; width: 125px; vertical-align:top;">Phone Number:</th>
											<td>
												<xsl:choose>
													<xsl:when test="string-length(@number) = 11">
													+<xsl:value-of select="substring(@number,1,1)"/>&#160;(<xsl:value-of select="substring(@number,2,3)"/>)&#160;<xsl:value-of select="substring(@number,5,3)"/>-<xsl:value-of select="substring(@number,8,4)"/>
													</xsl:when>
													<xsl:when test="string-length(@number) = 10">
													(<xsl:value-of select="substring(@number,1,3)"/>)&#160;<xsl:value-of select="substring(@number,4,3)"/>-<xsl:value-of select="substring(@number,7,4)"/>
													</xsl:when>
													<xsl:when test="string-length(@number) = 7">
														<xsl:value-of select="substring(@number,1,3)"/>-<xsl:value-of select="substring(@number,4,4)"/>
													</xsl:when>
													<xsl:otherwise>
														<xsl:value-of select="@number"/>
													</xsl:otherwise>
												</xsl:choose>
											</td>
										</tr>
										<tr>
											<th style="text-align: left; width: 125px; vertical-align:top;">Battery:</th>
											<td>
												<xsl:value-of select="@battery * 100"/>%</td>
										</tr>
										<tr>
											<th style="text-align: left; width: 125px; vertical-align:top;">Signal:</th>
											<td>
												<xsl:value-of select="@signal"/> dBm</td>
										</tr>
									</xsl:for-each>
								</table>
							</div>
						</div>
					</div>
				</div>
			</body>
		</html>
	</xsl:template>
</xsl:stylesheet>
